package com.css.zfzx.fwsjcj.modules.statistics.fw.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Util {

    //加法
    public static double add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).doubleValue();
    }

    //两位数相加,并保留两位小数
    public static double add(double v1, double v2,int v3) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        double v = b1.add(b2).doubleValue();
        BigDecimal b3 = new BigDecimal(v);
        double d1 = b3.setScale(v3, BigDecimal.ROUND_HALF_UP).doubleValue();
        return d1;
    }

    //减法
    public static double sub(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).doubleValue();
    }

    //万平方米转换为平方米
    public static double mul(double v1) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(10000.00));
        return b1.multiply(b2).doubleValue();
    }

    //将平方米转换为万平方米,保留两位小数
    public static double div(double v1) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(10000.00);
        if (b1.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO.doubleValue();
        }
        return b1.divide(b2, 2, RoundingMode.HALF_UP).doubleValue();
    }

    //小数位四舍五入,保留两位小数,不够则自动补齐
    public static String round(double v1) {
        BigDecimal b = new BigDecimal(Double.toString(v1));
        BigDecimal one = new BigDecimal("1");
        DecimalFormat decimalFormat = new DecimalFormat("0.00#");
        double v = b.divide(one, 2, RoundingMode.HALF_UP).doubleValue();
        String format = decimalFormat.format(v);
        return format;
    }

    //计算两个数的百分比,保留两位小数,不够则自动补齐
    public static String parsePercent(Object v1, Object v2) {
        Double d1 = Double.parseDouble(v1.toString());
        Double d2 = Double.parseDouble(v2.toString());
        if(d2==0.0){
            return "0.00%";
        }
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        double v = d1 / d2 * 100;
        String result = round(v) + "%";
        return result;
    }

    //四舍五入,保留两位小数
    public static Double round2(double v1) {
        BigDecimal b = new BigDecimal(Double.toString(v1));
        BigDecimal one = new BigDecimal("1");
        double v = b.divide(one, 2, RoundingMode.HALF_UP).doubleValue();
        return v;
    }


}